<!--关联设备-->
<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>

            <a-form-item label="所属牧场:" name="ranchId">
              <a-select
                show-search
                v-model:value="where.ranchId"
                placeholder="请选择所属牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="dlpId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm title="确定要绑定吗？" @confirm="bind(record)">
                  <a class="ele-text-danger">绑定</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

</template>

<script>
import { message } from 'ant-design-vue';
import {RanchApi} from "@/api/property/RanchApi";
import { DeviceLuoxunPulsationApi } from "@/api/property/DeviceLuoxunPulsationApi"
import { DeviceLuoxunMilkerPositionApi } from "@/api/property/milker/DeviceLuoxunMilkerPositionApi";
export default {
  name: 'DeviceManagerdeviceMilkerLinkPosition',
  components: {
  },
  props: {
    data: Object
  },
  async mounted() {
  },
  async created() {
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '设备id',
          dataIndex: 'recordId'
        },
        {
          title: '设备号',
          dataIndex: 'recordNo'
        },
        {
          title: '终端唯一码',
          dataIndex: 'clientNum'
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {isRelateMilker : 0},
      // 表格选中数据
      selection: [],
      ranchList : []
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reset() {
      this.where.ranchId = '';
      this.where.isRelateSwitch = 0;
      this.$nextTick(() => {
        this.reload();
      });
    },

    async bind(row)
    {
      let addParams = {
        pulsationRecordId : row.recordId,
        dlmId : this.data.dlmId
      };
      const result = await DeviceLuoxunMilkerPositionApi.add(addParams);
      message.success(result.message);
      this.reload();
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceLuoxunPulsationApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }

  }
};
</script>
